package cn.edu.sgu.www.mhxysy.controller.wenshi;

import cn.edu.sgu.www.mhxysy.vo.Imager;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.wenshi.WenshiDTO;
import cn.edu.sgu.www.mhxysy.dto.wenshi.ZuoqiWenshiDTO;
import cn.edu.sgu.www.mhxysy.entity.wenshi.Wenshi;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.wenshi.WenshiService;
import cn.edu.sgu.www.mhxysy.vo.wenshi.WenshiVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "纹饰管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/wenshi", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class WenshiController {

	private final WenshiService wenshiService;

	@Autowired
	public WenshiController(WenshiService wenshiService) {
		this.wenshiService = wenshiService;
	}

	@ApiOperation("克隆纹饰")
	@RequestMapping(value = "/clone", method = RequestMethod.POST)
	public JsonResult<Void> clone(
			@ApiParam(name = "wenshiId", value = "纹饰ID", required = true)
			@NotBlank(message = CustomMessages.WENSHI_ID_NOT_NULL) String wenshiId) {
		wenshiService.clone(wenshiId);

		return JsonResult.success("克隆成功");
	}

	@ApiOperation("镶嵌纹饰")
	@RequestMapping(value = "/inlay", method = RequestMethod.POST)
	public JsonResult<Void> inlay(@Validated ZuoqiWenshiDTO zuoqiWenshiDTO) {
		wenshiService.inlay(zuoqiWenshiDTO);

		String[] oprationArray = {"镶嵌", "取下"};
		Integer inlayStatus = zuoqiWenshiDTO.getInlayStatus();

		return JsonResult.success(oprationArray[inlayStatus] + "成功");
	}

	@ApiOperation("添加纹饰")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated WenshiDTO wenshiDTO) {
		wenshiService.insert(wenshiDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除纹饰")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "纹饰ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		wenshiService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改纹饰信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated WenshiDTO wenshiDTO) {
		wenshiService.updateById(wenshiDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("通过ID查询纹饰详情信息")
	@RequestMapping(value = "/selectDetail", method = RequestMethod.GET)
	public JsonResult<WenshiVO> selectDetail(
			@ApiParam(name = "wenshiId", value = "纹饰ID", required = true)
			@NotBlank(message = CustomMessages.WENSHI_ID_NOT_NULL) String wenshiId) {
		WenshiVO wenshiVO = wenshiService.selectDetail(wenshiId);

		return JsonResult.success(null, wenshiVO);
	}

	@ApiOperation("通过坐骑ID查询纹饰图片")
	@RequestMapping(value = "/selectByZuoqiId", method = RequestMethod.GET)
	public JsonResult<List<Imager>> selectByZuoqiId(
			@ApiParam(name = "zuoqiId", value = "坐骑ID", required = true)
			@NotBlank(message = CustomMessages.ZUOQI_ID_NOT_NULL) String zuoqiId) {
		List<Imager> list = wenshiService.selectByZuoqiId(zuoqiId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("分页条件查询纹饰列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<Wenshi>> selectByPage(Pager<Wenshi> pager) {
		Page<Wenshi> result = wenshiService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

	@ApiOperation("查询纹饰详情")
	@RequestMapping(value = "/selectDetails", method = RequestMethod.GET)
	public JsonResult<List<WenshiVO>> selectDetails(
			@ApiParam(name = "categoryId", value = "纹饰类型ID", required = false)
			@RequestParam(required = false) Integer categoryId) {
		List<WenshiVO> list = wenshiService.selectDetails(categoryId);

		return JsonResult.success(null, list);
	}

}